package org.itboys.mobile.service.mysql.front;

import org.itboys.commons.utils.time.TimeUtils;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.wallet.SundayWalletRecordDto;
import org.itboys.mobile.entity.mysql.wallet.SundayWalletRecord;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.ParamUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyang on 2018/1/3.
 * 圣代_钱包变动记录_service
 */
@Service
public class SundayFrontWalletRecordService extends BaseMySqlService<SundayWalletRecord> {


    /**
     * 获取钱包变动记录
     * @param memberId  用户id
     * @param flag  变动的对象(1-账户余额,2-积分,3-爱购币)
     * @param type //变动的类型:
                    // 账户余额(1-充值,2-提成,3-购物使用,4-提现),
                    //积分(5-积分兑换,6-购物获得,7-登录获得,8-评论获得,9-分享获得),
                    //爱购币(10-抵扣,11-购物获得,12-分享商品获得,13-展示到社区获得,14-评论获得)
     * @return  记录集合
     */
    public List<SundayWalletRecordDto> getRecord(Long memberId, Integer flag, Integer type, HttpServletRequest request){
        Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"");
        param.put("memberId",memberId);
        param.put("status",SundayCommonConstants.YES);
        param.put("sort_name","ct");
        param.put("sort_order","desc");
        //查询大类别记录(账户余额/积分/爱购币中的一种,包含了该项下面的小类别,比如账户余额包含了充值,提成,购物使用..等等)
        if(flag != null && flag != 0){
            List<Integer> types = new ArrayList<Integer>();
            if(flag == SundayCommonConstants.wallet_type_amount){//账户余额
                types.add(SundayCommonConstants.wallet_record_type_amount_cash);
                types.add(SundayCommonConstants.wallet_record_type_amount_commission);
                types.add(SundayCommonConstants.wallet_record_type_amount_recharge);
                types.add(SundayCommonConstants.wallet_record_type_amount_shop);
               // param.put("types",types);
            }else if(flag == SundayCommonConstants.wallet_type_score){//积分
                types.add(SundayCommonConstants.wallet_record_type_score_shop);
                types.add(SundayCommonConstants.wallet_record_type_score_comment);
                types.add(SundayCommonConstants.wallet_record_type_score_exchange);
                types.add(SundayCommonConstants.wallet_record_type_score_login);
                types.add(SundayCommonConstants.wallet_record_type_score_share);
                //param.put("types",types);
            }else if(flag == SundayCommonConstants.wallet_type_coin){//爱购币
                types.add(SundayCommonConstants.wallet_record_type_coin_comment);
                types.add(SundayCommonConstants.wallet_record_type_coin_deduction);
                types.add(SundayCommonConstants.wallet_record_type_coin_share);
                types.add(SundayCommonConstants.wallet_record_type_coin_shop);
                types.add(SundayCommonConstants.wallet_record_type_coin_show);
               // param.put("types",types);
            }else if(flag == SundayCommonConstants.wallet_type_commission){//我的提成
                types.add(SundayCommonConstants.wallet_record_type_amount_commission);
            }else if(flag == SundayCommonConstants.wallet_type_cash){//我的提现记录
                types.add(SundayCommonConstants.wallet_record_type_amount_cash);
            }else if(flag == SundayCommonConstants.wallet_type_conch){//我的贝壳交易记录
                types.add(SundayCommonConstants.wallet_record_type_conch_charge);
                types.add(SundayCommonConstants.wallet_record_type_conch_shop);
            }
            param.put("types",types);
        }else{//查询特定的小类别,比如获取提现记录.
            param.put("type",type);
        }

        List<SundayWalletRecord> result = super.select(param);

        //组装dto
        List<SundayWalletRecordDto> list = packageDtoMuilt(result,flag,type);
        return  list;
    }

    /**
     * 组装钱包变动记录dto(多)
     * @param recordList
     * @param type
     * @return
     */
    public List<SundayWalletRecordDto> packageDtoMuilt(List<SundayWalletRecord> recordList, Integer flag, Integer type){
        List<SundayWalletRecordDto> list = new ArrayList<SundayWalletRecordDto>();
        for(SundayWalletRecord record : recordList){
            SundayWalletRecordDto dto = packageDtoSingle(record,flag,type);
            list.add(dto);
        }
        return list;
    }


    /**
     * 组装钱包变动记录dto(单)
     * @param record
     * @param type
     * @return
     */
    public SundayWalletRecordDto packageDtoSingle(SundayWalletRecord record, Integer flag, Integer type){
        //BigDecimal negativeOne = new BigDecimal("-1");

        SundayWalletRecordDto dto = new SundayWalletRecordDto(
                record.getId(),record.getCreateTime(), record.getCreateDate(),
                record.getType(), record.getAmount(), record.getOrderNo(),record.getRemark());
        //如果是提现记录
        if(record.getType()==SundayCommonConstants.wallet_record_type_amount_cash){
            dto.setAccountStatus(record.getAccountStatus());
        }
        /*dto.setOrderNo(record.getOrderNo());
        dto.setTime(TimeUtils.format(record.getCt(),"yyyy/MM/dd HH:mm:ss"));
        dto.setType(record.getType());
        dto.setAccountStatus(record.getAccountStatus());
        dto.setAccountType(record.getAccountType());*/

        /** 账户余额-提现 */
        /*if(type == SundayCommonConstants.wallet_record_type_amount_cash){
            dto.setAccountNo(record.getAccountNo());
            dto.setAmount(new DecimalFormat("0.00").format(record.getAmount().multiply(negativeOne)));
        }else{
            dto.setAmount(new DecimalFormat("0.00").format(record.getAmount()));
        }*/
        return dto;
    }

}
